<template>
	<view>
		<view class="content">
			<view class="CountDown"> 获取定位</view>
			<u-button type="primary" @click="getlocation">获取定位</u-button>
			<view class="">
				您现在位于：{{province}}{{city}}{{district}}{{street}}{{street}}
			</view>



			<!-- 定位提示框 -->
			<u-popup mode="center" v-model="show" border-radius="20" width='574rpx' height='332rpx'>
				<view class="location">
					<view
						style="font-size: 36rpx; font-family: Source Han Sans CN; font-weight: 600; color: #222222;margin-top: 21rpx;">
						提示
					</view>
					<view class="tips">
						手机定位服务被关闭，无法获取， 会影响点名结果，请开启
					</view>
					<view class="btns">
						<u-button
							style="width: 216rpx;height: 80rpx;border-radius: 40rpx;background-color: #f2f4f6;color: #000000;font-size: 32rpx; margin-right: 46rpx;"
							type="primary" @click="close">取消</u-button>
						<u-button style="width: 216rpx;height: 80rpx;border-radius: 40rpx;font-size: 32rpx;"
							type="primary" @click="open">去开启</u-button>
					</view>
				</view>
			</u-popup>

		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				province: '',
				city: '',
				district: '',
				street: '',
				streetNum: ''

			}
		},
		onReady() {
			this.getlocation()
		},
		methods: {
			//获取定位
			getlocation() {
				console.log("00")
				uni.showLoading({
						title: "正在获取定位，请稍等..."
					}),
					uni.getLocation({
						geocode: true,
						type: 'wgs84',
						success: (res) => {
							if (res) {
								this.locationinfo = true
								uni.hideLoading()
								if (res.address) {
									let location = res.address
									this.province = location.province
									this.city = location.city
									this.district = location.district
									this.street = location.street
									this.streetNum = location.streetNum
								}
							}
						},
						fail: (err) => {
							console.log(err)
							this.show = true
							uni.showToast({
								title: '定位失败',
								icon: 'none'
							})
						}
					})
			},
			//去开启定位
			open() {
				this.popup = false
				let bool = false
				if (uni.getSystemInfoSync().platform == 'android') {
					var context = plus.android.importClass("android.content.Context");
					var locationManager = plus.android.importClass("android.location.LocationManager");
					var main = plus.android.runtimeMainActivity();
					var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
					bool = mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)
				}
				// ios平台
				if (uni.getSystemInfoSync().platform == 'ios') {
					var cllocationManger = plus.ios.import("CLLocationManager");
					var enable = cllocationManger.locationServicesEnabled();
					var status = cllocationManger.authorizationStatus();
					plus.ios.deleteObject(cllocationManger);
					bool = enable && status != 2
				}
				// 未开启定位功能
				if (bool === false) {
					// android平台
					if (uni.getSystemInfoSync().platform == 'android') {
						var Intent = plus.android.importClass('android.content.Intent');
						var Settings = plus.android.importClass('android.provider.Settings');
						var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
						var main = plus.android.runtimeMainActivity();
						main.startActivity(intent); // 打开系统设置GPS服务页面
						// this.again()
					}
					// ios平台
					if (uni.getSystemInfoSync().platform == 'ios') {
						var UIApplication = plus.ios.import("UIApplication");
						var application2 = UIApplication.sharedApplication();
						var NSURL2 = plus.ios.import("NSURL");
						var setting2 = NSURL2.URLWithString("App-Prefs:root=Privacy&path=LOCATION");
						application2.openURL(setting2);
						plus.ios.deleteObject(setting2);
						plus.ios.deleteObject(NSURL2);
						plus.ios.deleteObject(application2);
					}
				}
			},
			close() {
				uni.hideLoading()
				this.show=false
				uni.showToast({
					title:"获取定位失败，请开启！",
					icon:'none'
				})
			}





		}
	}
</script>

<style lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;

		.CountDown {

			margin-top: 400rpx;
			margin-bottom: 20rpx;
			font-size: 48rpx;
			font-weight: 600rpx;
		}


		.location {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: space-around;
			padding: 20rpx;

			.tips {
				text-align: center;
				margin: 25rpx 0;
				font-size: 28rpx;
				font-family: Source Han Sans CN;
				font-weight: 400;
				color: #606266;
			}

			.btns {
				display: flex;
				justify-content: space-between;
			}
		}



	}
</style>
